class Solution {
public:
    int ret;
    bool visit[300][300];
    int m, n;
    int numIslands(vector<vector<char>>& grid) {
        m = grid.size(), n = grid[0].size();
        for (int i = 0;i < m;i++)
        {
            for (int j = 0;j < n;j++)
            {
                if (grid[i][j] == '1' && visit[i][j] == false)
                {
                    visit[i][j] = true;
                    dfs(grid, i, j);
                    ret++;
                }
            }
        }
        return ret;
    }
    int row[4] = { -1,1,0,0 };
    int col[4] = { 0,0,-1,1 };
    void dfs(vector<vector<char>>& grid, int r, int c)
    {
        for (int k = 0;k < 4;k++)
        {
            int x = r + row[k], y = c + col[k];
            if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' && visit[x][y] == false)
            {
                visit[x][y] = true;
                dfs(grid, x, y);
            }
        }
    }
};